package com.zxy.ziems.server.energy.controller;

import com.zxy.ziems.server.constant.DateType;
import com.zxy.ziems.server.constant.enums.DictCodeEnum;
import com.zxy.ziems.server.dto.DataQueryDto;
import com.zxy.ziems.server.energy.service.DataChartDictService;
import com.zxy.ziems.server.vo.*;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 曲线图形
 *
 * @Copyright (C),
 * @ClassName: DataChartDictController
 * @Author: yanzhiwei
 * @Description:
 * @Date: 2024/6/18 16:44
 */
@RestController
@RequestMapping("/api/chart/dict")
public class DataChartDictController {

    @Autowired
    private DataChartDictService dataChartDictService;

    /**
     * 趋势图-本周期数据（分类下级所有和）
     *
     * @return
     */
    @GetMapping("/thisData")
    public ChartVo totalLine(DataQueryDto dataQueryDto) {
        return dataChartDictService.thisData(dataQueryDto);
    }

    /**
     * 趋势图-本周期数据（分类下级所有和，计算成本（费用/用量））
     *
     * @return
     */
    @GetMapping("/thisDataCost")
    public ChartVo thisDataCost(DataQueryDto dataQueryDto) {
        return dataChartDictService.thisDataCost(dataQueryDto);
    }

    /**
     * 趋势图-本周期数据（传下本机需要查询，查询所有传递据）
     *
     * @return
     */
    @GetMapping("/compareBarChart")
    public ChartVo totalTypeLine(DataQueryDto dataQueryDto) {
        return dataChartDictService.compareBarChart(dataQueryDto);
    }

    /**
     * 趋势图-本周期数据（传上级目录，查询所有下级数据）
     *
     * @return
     */
    @GetMapping("/childData")
    public ChartVo childData(DataQueryDto dataQueryDto) {
        if (dataQueryDto.isHalfHour() && dataQueryDto.getQueryType().equals(DateType.DAY)) {
            dataQueryDto.setQueryType(DateType.DAY_HALF_HOUR);
        }
        return dataChartDictService.childData(dataQueryDto);
    }


    /**
     * 室外温湿度导出
     *
     * @return
     */
    @GetMapping("/childData/export")
    public void exportChildData(HttpServletResponse response, DataQueryDto dataQueryDto) {
        dataChartDictService.exportChildData(response, dataQueryDto);
    }


    /**
     * 趋势图-分类下占比分析
     *
     * @return
     */
    @GetMapping("/totalTypePie")
    public List<PieChartVo> totalTypePie(DataQueryDto dataQueryDto) {
        return dataChartDictService.totalTypePie(dataQueryDto);
    }

    /**
     * 趋势图-分类下占比分析
     *
     * @return
     */
    @GetMapping("/radioTypePie")
    public List<PieChartVo> radioTypePie(DataQueryDto dataQueryDto) {
        final List<PieChartVo> pieChartVos = dataChartDictService.totalTypePie(dataQueryDto);
        BigDecimal all = pieChartVos.stream().map(m -> m.getValue()).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal allPercent = BigDecimal.ZERO;

        List<PieChartVo> pieChartVoList = new ArrayList<>();
        for (int i = 0; i < pieChartVos.size(); i++) {
            PieChartVo m = pieChartVos.get(i);
            PieChartPercentVo pieChartPercentVo = new PieChartPercentVo();
            BigDecimal divide = all.compareTo(BigDecimal.ZERO) != 0 ? m.getValue().divide(all, 4, RoundingMode.HALF_UP) : new BigDecimal(0.3333);
            divide = divide.setScale(4, RoundingMode.HALF_UP);
            BeanUtils.copyProperties(m, pieChartPercentVo);
            if (i == pieChartVos.size() - 1) {
                divide = new BigDecimal(1).subtract(allPercent).setScale(4, RoundingMode.HALF_UP);
            } else {
                allPercent = allPercent.add(divide);
            }
            pieChartPercentVo.setPercent(divide);
            pieChartVoList.add(pieChartPercentVo);
        }

        return pieChartVoList;
    }

    /**
     * 趋势图-水科制成率报表
     *
     * @return
     */
    @GetMapping("/compareWaterInfoReport")
    public List<WaterReportVo> compareWaterInfoReport(DataQueryDto dataQueryDto) {
        return dataChartDictService.compareWaterInfoReport(dataQueryDto);
    }

    /**
     * 趋势图-水科制成率
     *
     * @return
     */
    @GetMapping("/thisDataWaterInfoReport")
    public ChartVo thisDataWaterInfoReport(DataQueryDto dataQueryDto) {
        return dataChartDictService.thisDataWaterInfoReport(dataQueryDto);
    }

    /**
     * 趋势图-费用和用量
     *
     * @return
     */
    @GetMapping("/compareCodeAndInfo")
    public ChartCodeVo compreCodeAndInfo(DataQueryDto dataQueryDto) {
        return dataChartDictService.compareCodeAndInfo(dataQueryDto);
    }

    /**
     * 趋势图-下级单价
     *
     * @return
     */
    @GetMapping("/compareOneCodeAndInfo")
    public ChartVo compareOneCodeAndInfo(DataQueryDto dataQueryDto) {
        return dataChartDictService.compareOneCodeAndInfo(dataQueryDto);
    }

    /**
     * 趋势图-单例费用和用量
     *
     * @return
     */
    @GetMapping("/totalCodeAndInfo")
    public ChartCodeVo totalCodeAndInfo(DataQueryDto dataQueryDto) {
        return dataChartDictService.totalCodeAndInfo(dataQueryDto);
    }

    /**
     * 大屏-单耗和用量
     *
     * @return
     */
    @GetMapping("/totalInfoAndOneInfo")
    public ChartCodeVo totalInfoAndOneInfo(DataQueryDto dataQueryDto) {
        return dataChartDictService.totalInfoAndOneInfo(dataQueryDto);
    }

    /**
     * 大屏-冷量单价/cop
     *
     * @return
     */
    @GetMapping("/totalCoolInfoAndOneInfo")
    public ChartCodeVo totalCoolInfoAndOneInfo(DataQueryDto dataQueryDto) {
        dataQueryDto.setDictCode(DictCodeEnum.WATER_MADE.getCode());
        dataQueryDto.setItemName("原水量");
        return dataChartDictService.totalInfoAndOneInfo(dataQueryDto);
    }

    /**
     * 大屏-各分厂能耗占比
     *
     * @return
     */
    @GetMapping("/pageUi")
    public HashMap<String, Object> pageUi(DataQueryDto dataQueryDto) {
        return dataChartDictService.pageUi(dataQueryDto);
    }

    /**
     * 周环比图
     *
     * @return
     */
    @GetMapping("/dataChain")
    public ChartVo dataChain(DataQueryDto dataQueryDto) {
        return dataChartDictService.dataChain(dataQueryDto);
    }

}
